package com.cy.springcloud.configs;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Description: bean配置
 * @ClassName: BeanConfig
 * @Author: chenyu
 * @Date: 2025/1/3 15:03
 */
@Configuration //等价于一个Spring ApplicationContext.xml配置文件
public class BeanConfig {

    @LoadBalanced  //使用ribbon实现负载均衡调用
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * 覆盖掉原来ribbon默认的的轮询负载均衡策略
     * @return com.netflix.loadbalancer.IRule
     * @date 2025/1/6 14:59
    */
    @Bean
    public IRule irule() {
        //采用随机的负载均衡策略
        //return new RandomRule();
        //采用重试的负载均衡策略
        return new RetryRule();
    }
}
